var server = require('../../../utils/server');
var address_id;
Page({
    data:{
        region: ['广东省', '广州市', '海珠区'],
        customItem: '全部',
        name: '',
        tel: '',
        addr: '',
        yb: '',
        addrcode: [],
        addrinfo:{},
    },
	isDefault: false,
	
	nameChange: function(e) {
		
		var value = e.detail.value;
		
		this.setData({
			name: value
		});
	},
	addressChange: function(e) {
		
		var value = e.detail.value;
		
		this.setData({
			addr: value
		});
	},
	phoneChange: function(e) {
		
		var value = e.detail.value;
		
		this.setData({
			tel: value
		});
	},
	yzChange: function(e) {
		
		var value = e.detail.value;
		
		this.setData({
			yb: value
		});
	},
	getArea: function (pid, cb) {
		var that = this;
		

		
        

	},
	onLoad: function (options) {
        let _params={
            username:getApp().globalData.username,
            token: getApp().globalData.token,
            address_id: options.address_id
        };
        // server.postJSON('/person/address_info',_params,res=>{
        //     if(res.data.status==200){
        //         this.setData({
        //             id:res.data.list.id,
        //             name: res.data.list.name,
        //             tel: res.data.list.mobile,
        //             is_default: res.data.list.is_default,
        //             addr: res.data.list.address,
        //             region: [res.data.list.province_name, res.data.list.city_name, res.data.list.district_name],
        //             addrcode: [res.data.list.province, res.data.list.city, res.data.list.district],
        //         })
        //     }
        // })
    server.send('/person/address_info','POST',_params).then(res=>{
      if (res.status == 200) {
                this.setData({
                    id:res.list.id,
                    name: res.list.name,
                    tel: res.list.mobile,
                    is_default: res.list.is_default,
                    addr: res.list.address,
                    region: [res.list.province_name, res.list.city_name, res.list.district_name],
                    addrcode: [res.list.province, res.list.city, res.list.district],
                })
            }
    })
	},
    bindRegionChange: function (e) {
        this.setData({
            region: e.detail.value,
            addrcode: e.detail.code
        })
    },
    submit() {
        if (this.data.name && this.data.region[0] && this.data.tel && this.data.addr) {
            let _params = {
                username: getApp().globalData.username,
                token: getApp().globalData.token,
                name: this.data.name,
                mobile: this.data.tel,
                province: this.data.addrcode[0],
                city: this.data.addrcode[1],
                district: this.data.addrcode[2],
                address: this.data.addr,
                address_id:this.data.id
            }

            // server.postJSON('/person/addaddress', _params, res => {
            //     if (res.data.status == 200) {
            //         wx.navigateTo({
            //             url: '../list/list'
            //         })
            //     }
            // })
          server.send('/person/addaddress','POST',_params).then(res=>{
            if (res.status == 200) {
                    wx.navigateTo({
                        url: '../list/list'
                    })
                }
          })
        } else {
            wx.showToast({
                title: '请填写完整数据',
                icon: 'none'
            })
        }

    }
	
})